function fenceTabCtrl($scope, $rootScope, $http){
  'ngInject';
  $scope.activeAreaData = [];
  $scope.fenceAreaData = [];
  $scope.isAreaBtnActive = false;
  $scope.isOffworkTabActive = false;
  const PAGE_SIZE = 5;

  // 获取统计信息
  $scope.getFenceTabInfo = () => {
  	$http
  		.get(`${SERVER_CONTEXT}/api/electronFence/getFenceListByTenderId/${$scope.tenderId}`)
  		.success((resp) => {
  			if (resp.success) {
  				$scope.fenceAreaData = resp.data;
          $scope.fenceAreaData.push({fenceDataList:[],name: "代班领导", type: 4}); // 模拟代班领导数据
  			}
  		})
  }

  $scope.toggleFenceTab = (fenceAreaDataItem, forceTrue) => {
  	$scope.fenceAreaData.forEach((item) => {
  		if (fenceAreaDataItem == item) {
  			item.active = !!!item.active;
  			$scope.shouldShowAreaList = item.type == 3 ? false : item.active;
  			if (item.active) {
  				$scope.activeAreaData = item.fenceDataList;
  			}
  			if (item.type == 3 && item.active) {
  				$scope.isOffworkTabActive = true;
          $scope.activeAreaData = [];
  				$scope.getWorkerInfo(item);
  			} else if (item.type == 4 && item.active) { // 模拟代班领导
            $scope.isOffworkTabActive = true;
            $scope.shouldShowAreaList = false;
            $scope.activeAreaData = [];
            $scope.getBossInfo(item);
        } else {
  				$scope.isOffworkTabActive = false;
  			}
  			$scope.isFenceTabActive = item.active;
  		} else {
  			item.active = false;
  		}
  	});
  }

  $scope.activeFenceAreaBtn = (areaItemId) => {
  	$scope.activeAreaData.forEach((item) => {
  		if (item.id == areaItemId) {
  			if (!!!item.active) {
  				item.active = true;
  			}
  		} else {
  			item.active = false;
  		}
  	});
  }

  $scope.clickTablerow = (worker) => {
    $rootScope.$broadcast('setMapViewport', worker);
  }

  $scope.getWorkerInfo = (item, forceUpdate) => {
  	$http
  		.post(`${SERVER_CONTEXT}/api/electronFence/getWorkerListByFenceCondition/`,
  			{id: item.id, type: item.type, tenderId: $scope.tenderId})
  		.success((resp) => {
  			if (resp.success) {
  				$scope.queryWorkerInfo = resp.data || [];
				  if ($scope.queryWorkerInfo && $scope.queryWorkerInfo.length) {
				  	$scope.queryWorkerInfo.forEach((workerInfo, index) => {
				  		workerInfo.index = index + 1;
				  	});
				  	$scope.paginationWorkerList = $scope.queryWorkerInfo.slice(0, PAGE_SIZE);
				  } else {
				  	$scope.paginationWorkerList = [];
				  }
				  
  				$scope.paginationData = {
  					total : $scope.queryWorkerInfo.length,
  					pageNum: 1,
  					pageSize: PAGE_SIZE,
  				}
  			}
  		})
  }

  // 代班领导相关人员(模拟数据)
  $scope.getBossInfo = (item) => {
    var bossData = [
      {
        constructionTeamName:"无锡路桥集团股份有限公司",
        deviceStatus:"Offline",
        id:17579,
        name:"杨成飞",
        stationCategoryText:"项目总工"
      },
      {
        "constructionTeamName":"江苏纬信工程咨询有限公司",
        "name":"李治安",
        "id":"D2B4727F1C724D0AAA41F793B0D22BCB",
        "stationCategoryText":"监理",
        "deviceStatus":"Offline"
      },
      {
        "constructionTeamName":"江苏纬信工程咨询有限公司",
        "name":"陈清文",
        "id":"3AA6D678ED5B465C8E945DA93643825F",
        "stationCategoryText":"监理",
        "deviceStatus":"Offline"
      }
    ];
    $scope.queryWorkerInfo = bossData || [];
    if ($scope.queryWorkerInfo && $scope.queryWorkerInfo.length) {
      $scope.queryWorkerInfo.forEach((workerInfo, index) => {
        workerInfo.index = index + 1;
      });
      $scope.paginationWorkerList = $scope.queryWorkerInfo.slice(0, PAGE_SIZE);
    } else {
      $scope.paginationWorkerList = [];
    }
    
    $scope.paginationData = {
      total : $scope.queryWorkerInfo.length,
      pageNum: 1,
      pageSize: PAGE_SIZE,
    }

  }

  $scope.pageChange = () => {
  	// debugger;
  	let startIndex = ($scope.paginationData.pageNum - 1) * PAGE_SIZE;
  	let endIndex = startIndex + PAGE_SIZE;
  	$scope.paginationWorkerList = $scope.queryWorkerInfo.slice(startIndex, endIndex);
  }

  $scope.initPage = () => {
  	$scope.activeAreaData = [];
  	$scope.fenceAreaData = [];
  	$scope.queryWorkerInfo = [];
  	$scope.paginationWorkerList = [];
  	$scope.isAreaBtnActive = false;
  	$scope.isOffworkTabActive = false;
  	$scope.shouldShowAreaList = false;

  	$scope.getFenceTabInfo();
  }

  if ($rootScope.activeBid && $rootScope.activeBid.tenderId) {    
  	$scope.tenderId = $rootScope.activeBid.tenderId;
  	$scope.initPage();
  }

  $rootScope.$on('bidChange', function(e, bidInfo) {
  	$scope.tenderId = bidInfo.tenderId;
  	$scope.initPage();
  });

  $scope.$watch('activeAreaData', function(newVal, oldVal) {
  	// if ((!newVal || !newVal.length) && $scope.fenceAreaData && $scope.fenceAreaData.length) {
  	// 	$scope.fenceAreaData.forEach((fenceItem) => {
  	// 		if (fenceItem.type == 3) {
  	// 			$scope.getWorkerInfo(fenceItem);
  	// 		}
  	// 	})
  	// } else if (newVal) {
  	// 	// newVal.forEach((item) => {
  	// 	// 	if (item.active) {
  	// 	// 		$scope.getWorkerInfo(item);
  	// 	// 	}
  	// 	// });
  	// }
  	if (newVal && newVal.length) {
  		$scope.isAreaBtnActive = false;
  		newVal.forEach((item) => {
  			if (item.active) {
  				$scope.getWorkerInfo(item);
  				$scope.isAreaBtnActive = true;
  			}
  		});
  	}
  }, true);

  $scope.$on('clickFenceArea', function(e, fenceId) {
  	$scope.fenceAreaData.forEach((fenceItem) => {
  		fenceItem.fenceDataList.forEach((areaItem) => {
  			if (areaItem.id == fenceId) {
  				$scope.toggleFenceTab(fenceItem, true);
  				$scope.activeFenceAreaBtn(fenceId);
  			}
  		})
  	});
  })
}

export default fenceTabCtrl;